package com.fr.schedule.job;

import com.fr.base.FRContext;
import com.fr.base.dav.Env;
import com.fr.report.ReportWriteAttr;
import com.fr.report.ResultReport;
import com.fr.report.ResultWorkBook;
import com.fr.report.TemplateReport;
import com.fr.report.TemplateWorkBook;
import com.fr.report.script.Calculator;
import com.fr.schedule.ScheduleContext;
import com.fr.schedule.dao.JdbcDaoManager;
import com.fr.schedule.task.ScheduleTask;
import com.fr.schedule.util.ScheduleUtils;
import com.fr.third.org.quartz.Job;
import com.fr.third.org.quartz.JobDataMap;
import com.fr.third.org.quartz.JobExecutionContext;
import com.fr.third.org.quartz.JobExecutionException;
import com.fr.web.platform.entry.ReportletEntry;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

public class WriteReportletJob
  implements Job
{
  public void execute(JobExecutionContext paramJobExecutionContext)
    throws JobExecutionException
  {
    TemplateWorkBook localTemplateWorkBook;
    int i;
    Date localDate = paramJobExecutionContext.getScheduledFireTime();
    JobDataMap localJobDataMap = paramJobExecutionContext.getMergedJobDataMap();
    long l1 = localJobDataMap.getLong("__task__");
    ScheduleTask localScheduleTask = (ScheduleTask)ScheduleContext.getJdbcDaoManager().load(ScheduleTask.class, l1);
    long l2 = localJobDataMap.getLong("__reportlet__");
    ReportletEntry localReportletEntry = (ReportletEntry)ScheduleContext.getJdbcDaoManager().load(ReportletEntry.class, l2);
    String str = localReportletEntry.getReportletPath();
    if (str == null)
    {
      FRContext.getLogger().log(Level.WARNING, "Reporlet path is null.");
      return;
    }
    Calendar localCalendar = Calendar.getInstance();
    localCalendar.setTimeInMillis(localDate.getTime());
    Calculator localCalculator = Calculator.createCalculator();
    localCalculator.set("$date", localCalendar.getTime());
    Object[] arrayOfObject = ScheduleUtils.createCartesianParameterMapList(localScheduleTask.getParametersToArray(), localCalculator);
    List localList = (List)arrayOfObject[0];
    localCalculator.release();
    try
    {
      localTemplateWorkBook = FRContext.getCurrentEnv().readTemplate(str);
      if (localTemplateWorkBook == null)
        FRContext.getLogger().log(Level.WARNING, "Not found reportlet: " + str);
      for (i = 0; i < localList.size(); ++i)
        writeReportData(localTemplateWorkBook.execute4Write((Map)localList.get(i)), localTemplateWorkBook, Collections.EMPTY_MAP);
    }
    catch (Exception localException)
    {
      FRContext.getLogger().log(Level.WARNING, localException.getMessage(), localException);
      return;
    }
  }

  private static void writeReportData(ResultWorkBook paramResultWorkBook, TemplateWorkBook paramTemplateWorkBook, Map paramMap)
    throws Exception
  {
    for (int i = 0; i < paramTemplateWorkBook.getReportCount(); ++i)
      writeInnerReportData(paramResultWorkBook.getResultReport(i), paramTemplateWorkBook.getTemplateReport(i), paramMap);
  }

  private static void writeInnerReportData(ResultReport paramResultReport, TemplateReport paramTemplateReport, Map paramMap)
    throws Exception
  {
    ReportWriteAttr localReportWriteAttr = paramTemplateReport.getReportWriteAttr();
    if (localReportWriteAttr == null)
      return;
    localReportWriteAttr.submit(paramResultReport, paramTemplateReport.getTemplateWorkBook(), paramMap);
  }
}